package com.jhs.kjs.controller;

import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.servlet.ModelAndView;

import com.jhs.kjs.dto.AdminPointDTO;
import com.jhs.kjs.dto.MemberPagingDTO;
import com.jhs.kjs.dto.UserDTO;
import com.jhs.kjs.dto.UserViewDTO;
import com.jhs.kjs.handler.SpamPreventHandler;
import com.jhs.kjs.handler.exception.AldangMessegeException;
import com.jhs.kjs.model.UserModel;
import com.jhs.kjs.service.IAdminMemberService;
import com.jhs.kjs.service.IUserService;

@RequestMapping(value = "/admin")
@Controller
public class AdminMemberController extends CommonController {

	@Autowired
	IAdminMemberService adminMemberService;

	@Autowired
	IUserService userService;

	@RequestMapping(value = "/adminMemberMain", method = { RequestMethod.GET,
			RequestMethod.POST })
	public ModelAndView adminMemberMain() {
		ModelAndView mav = new ModelAndView("admin/member");
		MemberPagingDTO param = new MemberPagingDTO();
		int totalPage = adminMemberService.getCountUserList(param);
		mav.addObject("totalPage", totalPage);
		return mav;
	}

	@RequestMapping(value = "/userList", method = { RequestMethod.GET,
			RequestMethod.POST })
	public ModelAndView userList(@ModelAttribute("userId") MemberPagingDTO param) {
		ModelAndView mav = new ModelAndView("");
		List<UserDTO> userList = adminMemberService.getUserList(param);
		mav.addObject("userList", userList);
		int totalPage = adminMemberService.getCountUserList(param);
		mav.addObject("totalPage", totalPage);
		return mav;
	}
	
	
	

	@RequestMapping(value = "/memberView", method = { RequestMethod.GET,
			RequestMethod.POST })
	public ModelAndView memberView(HttpServletRequest request,
			@ModelAttribute("userId") String userId) {
		String viewName = "admin/member_view";
		ModelAndView mav = new ModelAndView(viewName);
		getUserInfoById(userId, mav);
		mav.addObject("buyTotal", adminMemberService.getBuyTotal(userId));
		mav.addObject("useTotal", adminMemberService.getUseTotal(userId));
		return mav;
	}

	
	@RequestMapping(value = "/member_write", method = { RequestMethod.GET,
			RequestMethod.POST })
	public ModelAndView memberWrite(HttpServletRequest request,
			@ModelAttribute("userId") String userId) {
		String viewName = "admin/member_write";
		ModelAndView mav = new ModelAndView(viewName);
		getUserInfoById(userId, mav);
		SpamPreventHandler.INSTANCE.addSpamFree(request, mav);
		return mav;
	}

	@RequestMapping(value = "/memberUpdate", method = { RequestMethod.GET,
			RequestMethod.POST })
	public ModelAndView memberUpdate(HttpServletRequest request,
			@ModelAttribute("userForm") UserDTO user) {
		ModelAndView mav = new ModelAndView();
		String userId = user.getUserId();
		String viewName = "/admin/member_write";
		
		try {
			SpamPreventHandler.INSTANCE.checkSpamFree(request, user);

			UserDTO savedUser = getUserInfoById(user.getUserId());
			savedUser.setUserPw(user.getUserPw());
			savedUser.setStatus(user.getStatus());

			UserModel userModel = new UserModel(userService);
			userModel.validateUser(savedUser);

			userService.updateUser(savedUser);

			viewName = "redirect:/admin/memberView?userId="+user.getUserId();
		} catch (AldangMessegeException e) {
			e.printStackTrace();
			String errorMsg = e.getMessage();
			errorMessageHandler(request, mav, errorMsg);
			getUserInfoById(userId, mav);
			mav.addObject("userId", userId);
			SpamPreventHandler.INSTANCE.addSpamFree(request, mav);
		} catch (Exception e) {
			e.printStackTrace();
			String errorMsg =  "알수 없는 에러가 발생하였습니다. 관리자에게 문의해 주세요.";
			errorMessageHandler(request, mav, errorMsg);
			getUserInfoById(userId, mav);
			mav.addObject("userId", userId);
			SpamPreventHandler.INSTANCE.addSpamFree(request, mav);
		}

		mav.setViewName(viewName);

		return mav;
	}

	private void getUserInfoById(String userId, ModelAndView mav) {
		mav.addObject("user", getUserInfoById(userId));
	}

	private UserViewDTO getUserInfoById(String userId) {
		return adminMemberService.getUserInfoByUserId(userId);
	}
	


	@RequestMapping(value = "/memberViewPointBuy", method = { RequestMethod.GET,
			RequestMethod.POST })
	public ModelAndView memberViewPointBuy(HttpServletRequest request,
			@ModelAttribute("pagingDTO") AdminPointDTO pagingDTO) {
		
		ModelAndView mav = new ModelAndView("");
		pagingDTO.setCustom_unit(3);
		List<AdminPointDTO> list = adminMemberService.getBuyPoint(pagingDTO);

		mav.addObject("buyTotal", adminMemberService.getBuyTotal(pagingDTO.getUserId()));
		mav.addObject("buyPoint", list);
		return mav;
	}


	@RequestMapping(value = "/memberViewPointUse", method = { RequestMethod.GET,
			RequestMethod.POST })
	public ModelAndView memberViewPointUse(HttpServletRequest request,
			@ModelAttribute("pagingDTO") AdminPointDTO pagingDTO) {
		
		ModelAndView mav = new ModelAndView("");
		pagingDTO.setCustom_unit(3);
		List<AdminPointDTO> list = adminMemberService.getUsePoint(pagingDTO);
		mav.addObject("usePoint", list);
		mav.addObject("useTotal", adminMemberService.getUseTotal(pagingDTO.getUserId()));
		return mav;
	}

	@RequestMapping(value = "/memberDeleteUser", method = { 	RequestMethod.POST })
	public ModelAndView memberDeleteUser(HttpServletRequest request,
			@ModelAttribute("pagingDTO") AdminPointDTO pagingDTO) {
		
		ModelAndView mav = new ModelAndView("");
		adminMemberService.memberDeleteUser(pagingDTO.getUserId());
		return mav;
	}
	
}
